package mediadisplay.timeline.controls 
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import mediadisplay.viewerarea.ScreenText;
	/**
	 * ...
	 * @author Anders Figenschow
	 */
	public class ControlPanel extends Sprite
	{
		private var tooltip:TextField;
		public var soundGraph:SoundGraph;
		private var currentElement:ScreenText;
		public var ignoreBox:TickBox;
		
		
		public function ControlPanel(soundGraph:SoundGraph) 
		{
			this.soundGraph = soundGraph;
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			var playButton:ControlButton = new ControlButton("play", "Resumes the track from the scanner point.  Spacebar also toggles play/pause!");
			addChild(playButton);
			playButton.addEventListener(MouseEvent.MOUSE_OVER, showToolTip);
			playButton.addEventListener(MouseEvent.MOUSE_OUT, hideToolTip);
			playButton.addEventListener(MouseEvent.CLICK, playButtonClickHandler);
			
			var pauseButton:ControlButton = new ControlButton("pause", "Pauses the track.  Spacebar also toggles play/pause!");
			addChild(pauseButton);
			pauseButton.x = 48;
			pauseButton.addEventListener(MouseEvent.MOUSE_OVER, showToolTip);
			pauseButton.addEventListener(MouseEvent.MOUSE_OUT, hideToolTip);
			pauseButton.addEventListener(MouseEvent.CLICK, pauseButtonClickHandler);
			
			var stopButton:ControlButton = new ControlButton("stop", "Stops the track and places scanner point at the beginning.  Hotkey: [ENTER]");
			addChild(stopButton);
			stopButton.x = 96;
			stopButton.addEventListener(MouseEvent.MOUSE_OVER, showToolTip);
			stopButton.addEventListener(MouseEvent.MOUSE_OUT, hideToolTip);
			stopButton.addEventListener(MouseEvent.CLICK, stopButtonClickHandler);
			
			var saveButton:ControlButton = new ControlButton("Save", "Save all keypoints to database.", 0x55ff55);
			addChild(saveButton);
			saveButton.x = -64;
			//saveButton.y = 100;
			saveButton.addEventListener(MouseEvent.MOUSE_OVER, showToolTip);
			saveButton.addEventListener(MouseEvent.MOUSE_OUT, hideToolTip);
			saveButton.addEventListener(MouseEvent.CLICK, saveButtonClickHandler);
			
			var editToggleButton:ControlButton = new ControlButton("edit", "Toggle edit mode on/off on focused media field (activate on focused timeline bar).");
			addChild(editToggleButton);
			editToggleButton.x = -64;
			editToggleButton.y = -60;
			editToggleButton.addEventListener(MouseEvent.MOUSE_OVER, showToolTip);
			editToggleButton.addEventListener(MouseEvent.MOUSE_OUT, hideToolTip);
			editToggleButton.addEventListener(MouseEvent.CLICK, editToggleButtonClickHandler);
			
			ignoreBox = new TickBox("Camera ignore");
			addChild(ignoreBox);
			ignoreBox.y = -20;
			ignoreBox.addEventListener(MouseEvent.CLICK, ignoreToggled);
			
			var textformat:TextFormat = new TextFormat();
			textformat.color = 0x777777;
			textformat.size = 12;
			
			tooltip = new TextField();
			addChild(tooltip);
			tooltip.y = -65;
			tooltip.width = 140;
			tooltip.multiline = true;
			tooltip.wordWrap = true;
			tooltip.defaultTextFormat = textformat;
			tooltip.autoSize = TextFieldAutoSize.LEFT;
			
		}
		public function setCurrentElement(currentElement:ScreenText):void {
			if (currentElement == null) return;
			this.currentElement = currentElement;
			ignoreBox.alpha = 1;
			
		}
		private function ignoreToggled(e:MouseEvent):void 
		{
			if (currentElement == null) {
				trace("no current element set.");
				return;
			}
			
			currentElement._ignore = ignoreBox.getTicked();
		}
		
		private function editToggleButtonClickHandler(e:MouseEvent):void 
		{
			soundGraph.editToggle();
		}
		
		private function saveButtonClickHandler(e:MouseEvent):void 
		{
			soundGraph.saveKeypoints();
		}
		
		private function stopButtonClickHandler(e:MouseEvent):void 
		{
			soundGraph.stop();
		}
		
		private function pauseButtonClickHandler(e:MouseEvent):void 
		{
			if (soundGraph.isPlaying) {
				soundGraph.pause();
			}
		}
		
		private function playButtonClickHandler(e:MouseEvent):void 
		{
			if (!soundGraph.isPlaying) {
				soundGraph.resume();
			}
		}
		
		private function hideToolTip(e:MouseEvent):void 
		{
			tooltip.text = "";
		}
		
		private function showToolTip(e:MouseEvent):void 
		{
			tooltip.text = e.currentTarget.getTooltip();
		}
		
	}

}